#!/bin/sh

VIP=192.168.100.10

RIP=(
192.168.100.1
192.168.100.2
)

while true
do
  for ((i=0;i<${#RIP[*]};i++))
  do
        port_count=$(nmap -p 80 ${RIP[$i]} 2>/dev/null|grep open|wc -l)
        if [ $port_count -ne 1 ];then
                if [ `ipvsadm -L|grep -w ${RIP[$i]}|wc -l` -ne 0 ];then
                ipvsadm -d -t $VIP:80 -r ${RIP[$i]}:80 >/dev/null 2>&1
                fi
        else
                if [ `ipvsadm -L|grep -w ${RIP[$i]}|wc -l` -ne 1 ];then
                ipvsadm -a -t $VIP:80 -r ${RIP[$i]}:80 >/dev/null 2>&1
                fi
        fi
  done
  sleep 2
done
